package com.zhangtai.modules.service.impl.dimission;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.DimissionAuditDao;
import com.zhangtai.modules.dto.DimissionInfoDto;
import com.zhangtai.modules.entity.employee.DimissionAuditEntity;
import com.zhangtai.modules.entity.employee.DimissionEntity;
import com.zhangtai.modules.service.dimission.DimissionAuditService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
public class DimissionAuditServiceImpl extends ServiceImpl<DimissionAuditDao, DimissionAuditEntity> implements DimissionAuditService {


    @Autowired
    private DimissionAuditDao dimissionAuditDao;
    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<DimissionAuditEntity> page = this.page(
                new Page<DimissionAuditEntity>(pageNum,pageSize),
                new QueryWrapper<DimissionAuditEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }



    @Override
    public  List<Map<String,Object>> queryPageList(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "hiredateStart", required = false) String hiredateStart,
            @RequestParam(value = "hiredateEnd", required = false) String hiredateEnd,
            @RequestParam(value = "jobLevelId", required = false) Long jobLevelId, //职级id
            @RequestParam(value = "lineId", required = false) Long lineId, //专业职级id
            @RequestParam(value = "jobSystemId", required = false) Long jobSystemId, //专业职级id
            @RequestParam(value = "status", required = false) Integer status, //审批状态
            @RequestParam(value = "upcomingStatus", required = false) Integer upcomingStatus, //审计待办状态
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<DimissionInfoDto> page = new Page<DimissionInfoDto>(pageNum,pageSize);
        List<Map<String,Object>> list = dimissionAuditDao.getAuditList(
                StringUtils.isEmpty(name)?null:name,
                StringUtils.isEmpty(code)?null:code,
                hiredateStart,hiredateEnd,jobLevelId,status,upcomingStatus,lineId,jobSystemId,page);

        return list;
    }

    @Override
    public  PageResult getDimissionAuditPageList(
            @RequestParam(value = "orgIds",required = false) List<Long> orgIds,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "hiredateStart", required = false) String hiredateStart,
            @RequestParam(value = "hiredateEnd", required = false) String hiredateEnd,
            @RequestParam(value = "jobLevelId", required = false) Long jobLevelId, //职级id
            @RequestParam(value = "lineId", required = false) Long lineId, //专业职级id
            @RequestParam(value = "jobSystemId", required = false) Long jobSystemId, //专业职级id
            @RequestParam(value = "status", required = false) Integer status, //审批状态
            @RequestParam(value = "upcomingStatus", required = false) Integer upcomingStatus, //审计待办状态
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

QueryWrapper<DimissionAuditEntity> queryWrapper = new QueryWrapper<DimissionAuditEntity>()
        .like(!StringUtils.isEmpty(name),"name",name)
        .like(!StringUtils.isEmpty(code),"code",code)
        .ge(!StringUtils.isEmpty(hiredateStart),"hiredate",hiredateStart)
        .le(!StringUtils.isEmpty(hiredateEnd),"hiredate",hiredateEnd)
        .eq(jobLevelId!=null,"job_level_id  ",jobLevelId)
        .eq(lineId!=null,"line_id",lineId)
        .eq(jobSystemId!=null,"job_system_id",jobSystemId)
        .eq(status!=null,"status",status)
        .eq(upcomingStatus!=null,"upcoming_status",upcomingStatus)
        .eq("is_delete",0);
        queryWrapper.and(wra->wra.in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds).or().
                eq("apply_code",code).or().eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)) .orderByDesc("create_time");
        IPage<DimissionAuditEntity> page = this.page(
                new Page<DimissionAuditEntity>(pageNum,pageSize),
                queryWrapper
        );
        return new PageResult(page);
    }

    @Override
    public DimissionAuditEntity getByIdE(Long id) {
            DimissionAuditEntity one = this.getOne(new QueryWrapper<DimissionAuditEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(DimissionAuditEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(DimissionAuditEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            DimissionAuditEntity entity = new DimissionAuditEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}